package org.example.buysystem.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.example.buysystem.common.Result;
import org.example.buysystem.constants.Constants;
import org.example.buysystem.entity.Good;
import org.example.buysystem.entity.GoodStandard;
import org.example.buysystem.entity.Standard;
import org.example.buysystem.service.GoodService;
import org.example.buysystem.service.StandardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/good")
@CrossOrigin
@Api(value = "商品模块",tags = {"商品模块"})
public class GoodController {

    @Autowired
    private GoodService goodService;

    @Autowired
    private StandardService standardService;

    @GetMapping
    @ApiOperation("获取商品列表")
    public Result findAllGoods(){

        return Result.success(goodService.findAllGoods());
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id获取商品")
    public Result findById(@PathVariable Long id){
        return Result.success(goodService.getGoodsById(id));
    }

    @GetMapping("/standard/{id}")
    @ApiOperation("根据id获取商品规格")
    public Result findStandardById(@PathVariable Long id){
        return Result.success(goodService.getStandardById(id));
    }


    @GetMapping("/fullPage")
    @ApiOperation("分页获取商品")
    public Result findGoodsByPage(
            @RequestParam(required = false,defaultValue = "1") Integer pageNum,
            @RequestParam(required = false,defaultValue = "10") Integer pageSize,
            @RequestParam(required = false,defaultValue = "") String searchText,
            @RequestParam(required = false) Integer categoryId){
        return Result.success(goodService.findGoodsByPage(pageNum,pageSize,searchText,categoryId));

    }

    @GetMapping("/recommend")
    @ApiOperation("修改推荐商品")
    public Result findRecommendGoods(
            @RequestParam Long id, @RequestParam Boolean isRecommend){

        return Result.success(goodService.updateRecommend(id,isRecommend));

    }

    @PostMapping
    @ApiOperation("保存商品")
    public Result addGood(@RequestBody Good good){
        System.out.println(good);
        return Result.success(goodService.addGood(good));
    }

    @PostMapping("/standard")
    @ApiOperation("保存商品规格信息")
    public Result addStandard(@RequestBody List<Standard> standards,@RequestParam int goodId){
        //删除旧纪录
        standardService.deleteAll(goodId);

        //插入新纪录
        for (Standard standard:standards){
            standard.setGoodId(goodId);
            if (!standardService.save(standard)){
                return Result.error(Constants.CODE_500,"保存失败");
            }

        }
        return Result.success();
    }


    @GetMapping("/page")
    @ApiOperation("分页查询商品")
    public Result findPage(
            @RequestParam(required = false,defaultValue = "1") Integer pageNum,
            @RequestParam(required = false,defaultValue = "10") Integer pageSize,
            @RequestParam(required = false,defaultValue = "") String searchText,
            @RequestParam(required = false) Integer categoryId){

return Result.success(goodService.findPage(pageNum,pageSize,searchText,categoryId));
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除商品")
    public Result delete(@PathVariable Long id){
        goodService.deleteGood(id);
        return Result.success();
    }

}
